import { defineStore } from "pinia";
import { userKey } from "./keys";

interface IUserState {
  menus: IMenuModel[];
  username: string;
}

export const useUserStore = defineStore(userKey, {
  state(): IUserState {
    return {
      menus: [],
      username: "",
    };
  },
  getters: {
    routes() {
      let menus: (IMenuModel & { children: IMenuModel[] })[] = [];
      menus = this.menus
        .filter((item1) => item1.parentId === 0)
        .map((item2) => ({
          ...item2,
          children: [],
        }));
      this.menus.forEach((item) => {
        let isChild = menus.find((i) => i.id === item.parentId);
        if (isChild) {
          isChild.children.push(item);
        }
      });
      return menus;
    },
    permission(): string[] {
      return this.menus.map((item) => item.name);
    },
  },
});
